package cn.lysoy.demo.service.impl;

import cn.lysoy.demo.domain.pojo.Goods;
import cn.lysoy.demo.domain.vo.GoodsVo;
import cn.lysoy.demo.mapper.GoodsMapper;
import cn.lysoy.demo.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LySoY
 * @since 2022-02-24
 */
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    private final GoodsMapper goodsMapper;
    /**
     * 方法功能描述：获取商品列表
     *
     * @return goodsList
     * @author: lysoy
     * @email: s2295938761@163.com
     * @date: 2022/02/24
     */
    @Override
    public List<GoodsVo> findGoodsVo() {
        return goodsMapper.findGoodsVo();
    }

    /**
     * 方法功能描述：获取单个商品信息
     *
     * @param goodsId GoodVo.id
     * @return GoodsVo
     * @author: lysoy
     * @email: s2295938761@163.com
     * @date: 2022/02/24
     */
    @Override
    public GoodsVo findGoodsVoByGoodsId(Long goodsId) {
        GoodsVo goodsVo = goodsMapper.findGoodsVoByGoodsId(goodsId);
        final Integer stockCount = goodsVo.getStockCount();
        int secKillStatus = 2;
        int remainSeconds = -1;
        if (stockCount > 0){
            Date startDate = goodsVo.getStartDate();
            Date endDate = goodsVo.getEndDate();
            Date currentDate = new Date();
            if (currentDate.before(endDate)) {
                // 秒杀尚未开始
                if (currentDate.before(startDate)) {
                    remainSeconds = (int) ((startDate.getTime() - currentDate.getTime())/1000);
                    secKillStatus = 0;
                } else {
                    // 秒杀正在进行
                    remainSeconds = 0;
                    secKillStatus = 1;
                }
            }
        }
        goodsVo.setRemainSeconds(remainSeconds);
        goodsVo.setSecKillStatus(secKillStatus);
        return goodsVo;
    }
}
